package jmind.core.lang;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import jmind.core.util.AppEnvironment;

public class SourceProperties implements IProperties {

    private static volatile SourceProperties instance;

    public static SourceProperties getDataSource() {
        if (instance == null) {
            synchronized (SourceProperties.class) {
                if (instance == null) {
                    instance = new SourceProperties(AppEnvironment.getEnvironment() + "-datasource.properties");
                }
            }
        }
        return instance;
    }

    private final Properties p = new Properties();

    public SourceProperties(String name) {
        InputStream in = null;
        try {
            in = getClass().getClassLoader().getResourceAsStream(name);
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public Map<?, ?> getProperties() {
        return p;
    }

    @Override
    public String getProperty(String key) {
        return p.getProperty(key);

    }

    @Override
    public String getProperty(String key, String defaultVal) {
        return p.getProperty(key, defaultVal);
    }

    public boolean hasProperty(String key) {
        return p.containsKey(key);
    }

}
